package com.hmjk.health.family;

import org.json.JSONException;
import org.json.JSONObject;

import com.hmjk.health.HealthApp;
import com.hmjk.health.R;
import com.hmjk.health.background.DataManager;
import com.hmjk.health.background.NetService;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class FamilyDetailEditorActivity extends Activity implements OnClickListener{
	private final String TAG = FamilyDetailEditorActivity.class.getSimpleName();
	private HealthApp mApp = null;
	private String fid = null;
	
	EditText editName = null, editAddr = null, editPhone = null;
	String localName, localAddr, localPhone;
	private boolean nameChanged = false;
	
	private void logd(String info){
		Log.d(TAG,info);
	}
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		logd("onCreate");
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.change_family_detail);
        
        mApp = (HealthApp) getApplication();
        
        editName = (EditText)findViewById(R.id.edit_name);
        editAddr = (EditText)findViewById(R.id.edit_addr);
        editPhone = (EditText)findViewById(R.id.edit_phone);
        
        Intent i = getIntent();
        fid = i.getStringExtra("fid");
        localName = i.getStringExtra("name");
        localAddr = i.getStringExtra("address");
        localPhone = i.getStringExtra("mobile");
        if((localName != null) && (localName.isEmpty() == false)){
        	editName.setText(localName);
        	editName.setSelection(localName.length());
        }
        if((localAddr != null) && (localAddr.isEmpty() == false)){
        	editAddr.setText(localAddr);
        	editAddr.setSelection(localAddr.length());
        }
        if((localPhone != null) && (localPhone.isEmpty() == false)){
        	editPhone.setText(localPhone);
        	editPhone.setSelection(localPhone.length());
        }
        
        ImageButton back = (ImageButton)findViewById(R.id.back);
		back.setOnClickListener(this);
		Button commit = (Button)findViewById(R.id.commit);
		commit.setOnClickListener(this);
	
	}
	
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			switch(msg.what){
			case HealthApp.MSG_NET_RESPONSE:{
				Bundle b = msg.getData();
				String stamp = b.getString("stamp","ooh!No!");
				boolean canceled = b.getBoolean("canceled", false);
				if(stamp.startsWith("COMMIT_FAMILY_DETAIL")){
					String sjson = b.getString("json");
					if(sjson != null){
						mApp.dismissWaiting();
						try {
							JSONObject json = new JSONObject(sjson);
							int code = json.getInt("code");
							if(code == 200){
								String state = json.getString("state");
								if(state.equals("00")){
									if(nameChanged){
										JSONObject j = new JSONObject();
										j.put("action", "changename");
										j.put("fid", fid);
										j.put("name", localName);
										mApp.getDataManager().commitData(DataManager.F_FAMILYLIST, j);
									}
									mApp.showAlertDialog(FamilyDetailEditorActivity.this, getResources().getString(R.string.family_commit_ok), R.string.ok, new OnClickListener() {
										
										@Override
										public void onClick(View arg0) {
											// TODO Auto-generated method stub
											setResult(RESULT_OK);
											finish();
										}
									});
									return;
								}
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						mApp.showAlertDialog(FamilyDetailEditorActivity.this, R.string.family_commit_failed);
					}
				}
			}break;
			}
		}
	};
	
	private void commitFamilyDetail(){
		//mApp.showWaitting(this, null);
		Intent si = new Intent(this, NetService.class);
		si.setAction(NetService.COMMIT_FAMILY_DETAIL);
		si.putExtra("uid", mApp.getUID());
		si.putExtra("fid", fid);
		si.putExtra("familyname", localName);
		si.putExtra("familyaddress", localAddr);
		si.putExtra("familyphoneno", localPhone);
		Time t = new Time();
		t.setToNow();
		String stamp = "COMMIT_FAMILY_DETAIL" + t.toString(); 
		si.putExtra("stamp",stamp);
		mApp.addNetHandler(stamp, mHandler);
		startService(si);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.back:{
			finish();
		}break;
		
		case R.id.commit:{
			String name = editName.getText().toString().trim();
			if(name.isEmpty() == false){
				nameChanged = !localName.equals(name);
				localName = name;
			}else {
				mApp.showAlertDialog(FamilyDetailEditorActivity.this, R.string.family_name_empty);
				return;
			}
			localAddr = editAddr.getText().toString().trim();
			localPhone = editPhone.getText().toString().trim();
			commitFamilyDetail();
		}break;
		}
	}

}
